<?php

namespace App\Http\Controllers\home;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Hash;
use Mail;
class ZhuceController extends Controller
{
   public function getIndex(){
        return view('home/zhuce/index');
   }
   public function postInsert(Request $request){
        if(session('code') != $request -> input('code')){
            return back() -> withInput() -> with('error','验证码错误!!!');
        }
        $s = $request -> only('email','password','repassword');
        if($s['password'] != $s['repassword']){
            return back() -> withInput() -> with('error','密码不一致');
        }
        $arr['email'] = $s['email'];
        $arr['token'] = str_random(50);
        $arr['password'] = Hash::make($s['password']);
        $arr['createTime'] = date('Y-m-d H:i:s',time());
        $id = DB::table('shop_users') -> insertGetId($arr);
        if($id > 0){
            self::mailto($arr['email'],$id,$arr['token']);
        }
   }
   static public function mailto($email,$id,$token){
        Mail::send('./home/email/index', ['id'=>$id,'token'=>$token], function ($m) use ($email) {
            $m->to($email)->subject('发送邮件！！！');
        });
   }
   public function getJihuo(Request $request){
        $s = $request -> only(['id','token']);
        $arr['status'] = 2;
        $res = DB::table('shop_users') -> where('a_uid',$s['id']) -> where('token',$s['token']) -> update($arr);
        if($res){
            return redirect('/home/login/index') -> with('success','激活成功');
        }else{
            return back() -> withInput() -> with('error','激活失败');
        }
   }
}
